<template>
  <div class="ele-body">
    <el-card shadow="never">
      <div slot="header" class="clearfix">
        <span>信息流访问记录</span>
      </div>
      <!-- 搜索表单 -->
      <el-form
        :model="where"
        class="ele-form-search"
        label-width="75px"
        @keyup.enter.native="reload"
        @submit.native.prevent
      >
        <div class="app-container">
          <div class="filter-container">
            <el-select
              v-model="where.bazaarId"
              clearable
              placeholder="推广市场"
              style="width: 180px; margin-right: 6px"
              @change="changebazaarId"
            >
              <template v-for="item in bazaarList">
                <el-option
                  v-if="item.status === 1"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </template>
            </el-select>

            <el-select
              v-model="where.username"
              clearable
              placeholder="推广账户"
              style="width: 160px; margin-right: 6px"
              @change="reload"
            >
              <template v-for="item in expandList">
                <el-option
                  v-if="item.status === 1"
                  :key="item.userid"
                  :label="item.username"
                  :value="item.userid"
                />
              </template>
            </el-select>

            <el-select
              v-model="where.accessPage"
              class="filter-item"
              clearable
              placeholder="访问页面"
              style="width: 120px"
              @change="reload"
              @clear="reload"
            >
              <el-option
                v-for="item in accessPageList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>

            <el-date-picker
              v-model="daterange"
              :clearable="false"
              :picker-options="pickerOptions"
              end-placeholder="数据结束日期"
              range-separator="-"
              start-placeholder="数据开始日期"
              style="width: 226px; margin-right: 6px"
              type="datetimerange"
              unlink-panels
              value-format="yyyy-MM-dd HH:mm:ss"
              @change="onDateRangeChoose"
            />

            <el-button
              icon="el-icon-search"
              style="margin-right: 6px"
              type="primary"
              @click="reload"
              >搜索
            </el-button>

            <el-button
              icon="el-icon-refresh"
              style="margin-left: 2px"
              @click="reset"
              >重置
            </el-button>
          </div>
        </div>
      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :columns="columns"
        :datasource="url"
        :max-height="defaultHeight"
        :parse-data="parseData"
        :selection.sync="selection"
        :where="where"
      >
        <!-- 表头工具栏 -->
        <template slot="toolbar">
          <el-button
            v-if="$hasPermission('feedaccess:clear')"
            :loading="deleteLoading"
            icon="el-icon-download"
            size="small"
            type="danger"
            @click="deleteData"
            >删除三个月之前日志
          </el-button>
        </template>

        <!-- 推广市场列 -->
        <template slot="bazaarId" slot-scope="{ row }">
          <template v-for="item in bazaarList">
            <span v-if="row.bazaarId === item.id" :key="item.id">
              {{ item.name }}
            </span>
          </template>
        </template>

        <!-- IP地址列 -->
        <template slot="ipAddr" slot-scope="{ row }">
          <span>{{ row.addr }}</span>
        </template>

        <!-- 停留时长列 -->
        <template slot="staySecond" slot-scope="{ row }">
          {{ formatSeconds(row.staySecond) }}
        </template>

        <!-- 落地页列 -->
        <template slot="url" slot-scope="{ row }">
          <el-link :href="row.url" target="_blank" type="primary">{{
            row.url
          }}</el-link>
        </template>

        <!-- 访问页面列 -->
        <template slot="accessPage" slot-scope="{ row }">
          <span
            :class="['', 'ele-text-success', 'ele-text-info'][row.accessPage]"
          >
            {{ ["", "真页面", "假页面"][row.accessPage] }}
          </span>
        </template>

        <!-- 停留位置列 -->
        <template slot="offset" slot-scope="{ row }">
          <el-link
            :underline="false"
            v-if="row.offset"
            icon="el-icon-s-grid"
            type="primary"
            @click="openDetails(row)"
            >查看
          </el-link>
        </template>

        <!-- 落地页列 -->
        <template slot="accessTime" slot-scope="{ row }">
          <ele-dot
            v-if="formatTime(row.accessTime)"
            :ripple="ripple"
            size="8px"
            text="在线"
            type="success"
          />
          <ele-dot
            v-if="!formatTime(row.accessTime)"
            :ripple="ripple"
            size="8px"
            text="离线"
            type="info"
          />
        </template>
      </ele-pro-table>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "feedAccess",
  data() {
    return {
      // 当前地域数据
      regionTarget: null,
      // 是否显示地域弹窗
      showRegionTarget: false,
      // 账户列表
      expandList: [],
      // 账户列表
      tempexpandList: [],
      //市场列表
      bazaarList: [],
      // 访问页面
      accessPageList: [
        { value: 1, label: "真页面" },
        { value: 2, label: "假页面" },
      ],
      // 清除状态按钮
      deleteLoading: false,
      // 表格高度
      defaultHeight: 0,
      // 筛选日期范围
      daterange: this.getDaterange(),
      // 日期时间选择器快捷项
      pickerOptions: {
        shortcuts: [
          {
            text: "今天",
            onClick(picker) {
              let start = new Date();
              start.setTime(start.getTime());
              start =
                start.getFullYear() +
                "-" +
                (start.getMonth() + 1) +
                "-" +
                start.getDate() +
                " 00:00:00";
              let end = new Date();
              end.setTime(end.getTime() + 24 * 60 * 60 * 1000);
              end =
                end.getFullYear() +
                "-" +
                (end.getMonth() + 1) +
                "-" +
                end.getDate() +
                " 00:00:00";
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "昨天",
            onClick(picker) {
              let start = new Date();
              start.setTime(start.getTime() - 24 * 60 * 60 * 1000);
              start =
                start.getFullYear() +
                "-" +
                (start.getMonth() + 1) +
                "-" +
                start.getDate() +
                " 00:00:00";
              let end = new Date();
              end.setTime(end.getTime());
              end =
                end.getFullYear() +
                "-" +
                (end.getMonth() + 1) +
                "-" +
                end.getDate() +
                " 00:00:00";
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一周",
            onClick(picker) {
              let start = new Date();
              start.setTime(start.getTime() - 7 * (24 * 60 * 60 * 1000));
              start =
                start.getFullYear() +
                "-" +
                (start.getMonth() + 1) +
                "-" +
                start.getDate() +
                " 00:00:00";
              let end = new Date();
              end.setTime(end.getTime());
              end =
                end.getFullYear() +
                "-" +
                (end.getMonth() + 1) +
                "-" +
                end.getDate() +
                " 00:00:00";
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近半月",
            onClick(picker) {
              let start = new Date();
              start.setTime(start.getTime() - 15 * (24 * 60 * 60 * 1000));
              start =
                start.getFullYear() +
                "-" +
                (start.getMonth() + 1) +
                "-" +
                start.getDate() +
                " 00:00:00";
              let end = new Date();
              end.setTime(end.getTime());
              end =
                end.getFullYear() +
                "-" +
                (end.getMonth() + 1) +
                "-" +
                end.getDate() +
                " 00:00:00";
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              let start = new Date();
              start.setTime(start.getTime() - 30 * (24 * 60 * 60 * 1000));
              start =
                start.getFullYear() +
                "-" +
                (start.getMonth() + 1) +
                "-" +
                start.getDate() +
                " 00:00:00";
              let end = new Date();
              end.setTime(end.getTime());
              end =
                end.getFullYear() +
                "-" +
                (end.getMonth() + 1) +
                "-" +
                end.getDate() +
                " 00:00:00";
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              let start = new Date();
              start.setTime(start.getTime() - 90 * (24 * 60 * 60 * 1000));
              start =
                start.getFullYear() +
                "-" +
                (start.getMonth() + 1) +
                "-" +
                start.getDate() +
                " 00:00:00";
              let end = new Date();
              end.setTime(end.getTime());
              end =
                end.getFullYear() +
                "-" +
                (end.getMonth() + 1) +
                "-" +
                end.getDate() +
                " 00:00:00";
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近半年",
            onClick(picker) {
              let start = new Date();
              start.setTime(start.getTime() - 180 * (24 * 60 * 60 * 1000));
              start =
                start.getFullYear() +
                "-" +
                (start.getMonth() + 1) +
                "-" +
                start.getDate() +
                " 00:00:00";
              let end = new Date();
              end.setTime(end.getTime());
              end =
                end.getFullYear() +
                "-" +
                (end.getMonth() + 1) +
                "-" +
                end.getDate() +
                " 00:00:00";
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一年",
            onClick(picker) {
              let start = new Date();
              start.setTime(start.getTime() - 365 * (24 * 60 * 60 * 1000));
              start =
                start.getFullYear() +
                "-" +
                (start.getMonth() + 1) +
                "-" +
                start.getDate() +
                " 00:00:00";
              let end = new Date();
              end.setTime(end.getTime());
              end =
                end.getFullYear() +
                "-" +
                (end.getMonth() + 1) +
                "-" +
                end.getDate() +
                " 00:00:00";
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      // 表格数据接口
      url: "/FeedAccess/method",
      // 表格列配置
      columns: [
        {
          columnKey: "index",
          type: "index",
          width: 80,
          label: "序号",
          align: "center",
          showOverflowTooltip: true,
        },
        {
          prop: "username",
          label: "推广账户",
          sortable: "custom",
          showOverflowTooltip: true,
        },
        {
          prop: "bazaarId",
          label: "推广市场",
          slot: "bazaarId",
          sortable: "custom",
          showOverflowTooltip: true,
        },
        {
          prop: "domain",
          label: "访问域名",
          sortable: "custom",
          showOverflowTooltip: true,
        },
        {
          prop: "accessPage",
          label: "访问页面",
          sortable: "custom",
          slot: "accessPage",
          showOverflowTooltip: true,
        },
        {
          prop: "staySecond",
          label: "浏览时长",
          sortable: "custom",
          showOverflowTooltip: true,
          slot: "staySecond",
        },
        {
          prop: "ipAddr",
          slot: "ipAddr",
          label: "IP地域",
          sortable: "custom",
          showOverflowTooltip: true,
        },
        {
          prop: "ip",
          label: "访问IP",
          sortable: "custom",
          showOverflowTooltip: true,
        },
        {
          prop: "dpi",
          label: "分辨率",
          sortable: "custom",
          showOverflowTooltip: true,
        },
        {
          prop: "device",
          label: "设备",
          sortable: "custom",
          resizable: false,
        },
        {
          prop: "browser",
          label: "浏览器",
          sortable: "custom",
          resizable: false,
        },
        {
          prop: "offset",
          slot: "offset",
          label: "停留位置",
          sortable: "custom",
          resizable: false,
        },
        {
          prop: "url",
          slot: "url",
          label: "落地页",
          sortable: "custom",
          showOverflowTooltip: true,
        },
        {
          width: 140,
          prop: "accessTime",
          label: "访问时间",
          sortable: "custom",
          resizable: false,
        },
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
    };
  },
  created() {
    window.addEventListener("resize", this.getHeight);
    this.getHeight();
  },

  methods: {
    openDetails(obj) {
      let array = [];
      let str = obj.offset + "";
      if (str) {
        if (str.indexOf("|") > -1) {
          array = str.split("|");
        } else {
          array.push(str);
        }
        let msgArray = [];
        for (let i = 0; i < array.length; i++) {
          msgArray.push("滑动页面位置: " + array[i]);
        }
        this.$alert(msgArray.join("<br>"), "滑动页面位置查看", {
          confirmButtonText: "关闭",
          dangerouslyUseHTMLString: true,
          customClass: "el-message-plan",
        });
      }
    },

    /*市场被改变*/
    changebazaarId(id) {
      let arrayList = [];
      this.where.username = "";
      for (let i = 0; i < this.tempexpandList.length; i++) {
        if (this.tempexpandList[i].bazaarId === id) {
          arrayList.push(this.tempexpandList[i]);
        }
      }
      this.expandList = arrayList;
      this.reload();
    },

    /*时间格式化*/
    formatTime(value) {
      const format = new Date().getTime() - new Date(value).getTime();
      if (format >= 12000) {
        return false;
      }
      return true;
    },

    /* 删除日志 */
    deleteData() {
      if (!this.$hasPermission("feedaccess:clear")) {
        return this.$message.error("你没有权限执行此操作!");
      }
      this.$confirm("该操作不可恢复, 确定是否删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        this.deleteLoading = true;
        this.$http
          .delete("/FeedAccess/clear")
          .then((res) => {
            this.deleteLoading = false;
            if (res.data.code === 0) {
              this.$message({ type: "success", message: res.data.msg });
              this.reload();
            } else {
              this.$message.error(res.data.msg);
            }
          })
          .catch((e) => {
            this.deleteLoading = false;
            this.$message.error(e.message);
          });
      });
    },

    /*默认时间*/
    getDaterange() {
      let start = new Date();
      start.setTime(start.getTime());
      start =
        start.getFullYear() +
        "-" +
        (start.getMonth() + 1) +
        "-" +
        start.getDate() +
        " 00:00:00";
      let end = new Date();
      end.setTime(end.getTime() + 24 * 60 * 60 * 1000);
      end =
        end.getFullYear() +
        "-" +
        (end.getMonth() + 1) +
        "-" +
        end.getDate() +
        " 00:00:00";
      return [start, end];
    },

    /* 日期选择改变回调 */
    onDateRangeChoose() {
      if (this.daterange && this.daterange.length === 2) {
        this.where.createTimeStart = this.daterange[0];
        this.where.createTimeEnd = this.daterange[1];
      } else {
        this.where.createTimeStart = null;
        this.where.createTimeEnd = null;
      }
    },

    /*设置表格高度*/
    getHeight() {
      this.defaultHeight = window.innerHeight - 360;
    },

    /*表格数据加载*/
    parseData(res) {
      if (res.code === 0) {
        this.tempexpandList = res.expand;
        this.bazaarList = res.bazaar;
      }
      return {
        code: res.code,
        data: res.data,
        count: res.count,
        msg: res.msg,
      };
    },

    /* 刷新表格 */
    reload(isPage) {
      let obj = { where: this.where };
      if (!isPage) {
        obj.page = 1;
      }
      this.$refs.table.reload(obj);
    },

    /* 重置搜索 */
    reset() {
      for (let key in this.where) {
        this.where[key] = null;
      }
      let daterange = this.getDaterange();
      if (daterange.length === 2) {
        this.where.createTimeStart = daterange[0];
        this.where.createTimeEnd = daterange[1];
      }
      this.daterange = daterange;
      this.reload();
    },

    /*转分秒格式化*/
    formatSeconds(value) {
      const time = parseInt(value, 0);
      if (time <= 60) {
        return time + "秒";
      }
      let result = "";
      let minute = 0; // 分
      if (time > 60) {
        minute = parseInt(time / 60, 0);
        result += minute + "分";
      }
      let seconds = time - minute * 60;
      if (seconds > 0) {
        result += seconds + "秒";
      }
      return result;
    },
  },
};
</script>

<style scoped></style>
